package dt.inlee.system.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.BusinessDictionary;
import dt.inlee.domain.BusinessDictionaryDetail;

/**
 * 
 * @功能说明：业务字典数据访问层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-5
 */
@Repository("businessDictionaryDao")
public class BusinessDictionaryDao extends BaseDao {

	/**
	 * 根据条件获取业务字典参数总记录数
	 * @param cnName
	 * @param enName
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public int getBusinessDictionaryTotalRows(String cnName, String enName) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				BusinessDictionary.class);
		if (!BaseUtil.isEmpty(cnName))
			criteria.add(Restrictions.like("cnName", "%" + cnName + "%"));
		if (!BaseUtil.isEmpty(enName))
			criteria.add(Restrictions.like("enName", "%" + enName + "%"));
		criteria.setProjection(Projections.countDistinct("id"));
		List<Integer> count = criteria.list();
		return count.get(0).intValue();
	}

	/**
	 * 根据条件获取业务字典参数记录
	 * @param cnName
	 * @param enName
	 * @param curPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<BusinessDictionary> findAllBusinessDictionaries(String cnName, String enName, int curPage, int pageSize)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				BusinessDictionary.class);
		if (!BaseUtil.isEmpty(cnName))
			criteria.add(Restrictions.like("cnName", "%" + cnName + "%"));
		if (!BaseUtil.isEmpty(enName))
			criteria.add(Restrictions.like("enName", "%" + enName + "%"));
		if (curPage > 0 && pageSize > 0) {
			criteria.setFirstResult((curPage - 1) * pageSize);
			criteria.setMaxResults(pageSize);
		}
		return criteria.list();
	}

	/**
	 * 通过业务字典ID、英文名称和中文名称查找业务字典
	 * 
	 * @param params
	 * @return  BusinessDictionary
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public BusinessDictionary findBusinessDictionaryByIdAndName(String id, String enName, String cnName)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				BusinessDictionary.class);
		criteria.add(Restrictions.ne("id", id));
		criteria.add(Restrictions.or(Restrictions.eq("enName", enName), Restrictions.eq("cnName", cnName)));
		List<BusinessDictionary> businessDictionaries = criteria.list();
		if (!BaseUtil.isEmpty(businessDictionaries) && businessDictionaries.size() > 0)
			return businessDictionaries.get(0);
		return null;

	}

	/**
	 * 根据中英文名获取业务字典
	 * @param enName
	 * @param cnName
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public BusinessDictionary findBusinessDictionaryByName(String enName, String cnName) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				BusinessDictionary.class);
		criteria.add(Restrictions.or(Restrictions.eq("enName", enName), Restrictions.eq("cnName", cnName)));
		List<BusinessDictionary> businessDictionaries = criteria.list();
		if (!BaseUtil.isEmpty(businessDictionaries) && businessDictionaries.size() > 0)
			return businessDictionaries.get(0);
		return null;
	}

	/**
	 * 通过业务字典ID获取业务字典明细
	 * @param id
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<BusinessDictionaryDetail> findBusinessDictionaryDetailsByBusiId(String id) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				BusinessDictionaryDetail.class);
		criteria.add(Restrictions.eq("businessDictionary.id", id));
		criteria.addOrder(Order.asc("value"));
		return criteria.list();
	}
}
